home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-02-26 | 5.5 KB | 221 lines | [TEXT/MPS ] |
- /*
- File: UTidyApplication.cp
-
- Contains: TTidyApplication::Free() and functions and member functions
- which it calls. For use with MacApp 3.0.1.
-
- Written by: Adam Wildavsky
-
- Copyright: © 1994 by Adam Wildavsky
-
- Change History (most recent first):
-
- 2/26/94 aw Propagated a change from the 3.1 version: added
- #ifdef qCleanUpAtQuit to TTidyApplication::Free().
- Added a comment.
- 2/24/94 aw Trimmed blanks, added punctuation. No code changes.
- 1/23/94 aw Baseline.
-
- To Do:
- Clean up non-object allocations
- */
-
-
- #ifndef __UTIDYAPPLICATION__
- #include "UTidyApplication.h"
- #endif
-
- #ifndef __MACAPP__
- #include <MacApp.h>
- #endif
-
-
- #pragma segment AClose
- static void DeInitUMenuMgr()
- {
- /*******
-
- Clean up the objects allocated by InitUMenuMgr, in reverse order of allocation.
-
- ********/
-
- extern TObject * gMenuTable; // Actually these are all TObject descendants,
- extern TObject * gCmdTable; // but this will work! If we had declared them
- extern TObject * gMenuIDList; // as their actual types we'd have cast them
- // to TObjects anyway for FreeIfObject().
-
- gMenuTable = FreeIfObject(gMenuTable);
- gCmdTable = FreeIfObject(gCmdTable);
- gMenuIDList = FreeIfObject(gMenuIDList);
- }
-
-
- #pragma segment AClose
- static void DeInitUObject()
- {
- /*******
-
- Clean up the object allocated by InitUObject
-
- ********/
-
- extern TObject * pOrderedClassIds; // Actually it's a TNameOrderedClassIDs, but this will work!
- pOrderedClassIds = FreeIfObject(pOrderedClassIds);
- }
-
-
- #pragma segment AClose
- static void DeInitUDialog()
- {
- /*******
-
- Clean up the objects allocated by InitUDialog, in reverse order of allocation.
-
- ********/
-
- // This test is necessary because gFloatingTEManager and gParamTxt
- // are -not- initialized to NULL when they are defined in UDialog.cp
- if (gUDialogInitialized)
- {
- gFloatingTEManager = (TFloatingTEManager *) FreeIfObject(gFloatingTEManager);
-
- extern TAssociation * gParamTxt; // It should be empty now
- gParamTxt = (TAssociation *) FreeIfObject(gParamTxt);
- }
- }
-
-
- #pragma segment AClose
- static void DeInitUMacApp()
- {
- /*******
-
- Clean up the objects allocated by InitUMacApp, in reverse order of allocation.
-
- ********/
-
- DeInitUDialog();
- DeInitUMenuMgr();
-
- extern TDynamicArray * gSignatures;
- gSignatures = (TDynamicArray *) FreeIfObject(gSignatures);
-
- if (gGlobalContext != NULL)
- {
- gGlobalContext->fOtherObjects->FreeAll();
- gGlobalContext = (TContext *) FreeIfObject(gGlobalContext);
- }
-
- gPrintHandler = (TPrintHandler *) FreeIfObject(gPrintHandler);
- gNullPrintHandler = (TPrintHandler *) FreeIfObject(gNullPrintHandler);
-
- DeInitUObject();
- }
-
-
- #pragma segment AClose
- static void LastChance()
- {
- // Set a breakpoint here in order to check for memory leaks.
- // In Jasik do an "Objects by Time".
-
- #if qDebug
- ProgramBreak("It's time to check for memory leaks!");
- #endif
- }
-
-
- #pragma segment AClose
- void TTidyApplication::CleanUpAfterIApplication()
- {
- /*******
-
- Clean up the objects allocated by TApplication::IApplication, in reverse order of allocation.
-
- ********/
-
- gMenuBarManager = (TMenuBarManager *) FreeIfObject(gMenuBarManager);
- gMacAppDependencies = (TDependencySpace *) FreeIfObject(gMacAppDependencies);
-
- fPendingReplyList = (TEventList *) FreeListIfObject(fPendingReplyList);
-
- // This rigamarole is necessary because TEvent::Free() checks to see whether
- // the event is still on the event list.
- if (fEventList != NULL)
- {
- TEvent * anEvent;
-
- while ((anEvent = (TEvent *) fEventList->Last()) != NULL)
- {
- fEventList->AtDelete(fEventList->GetSize());
- anEvent->Free();
- }
-
- fEventList->Free();
- }
-
- fDocumentList = (TList *) FreeListIfObject(fDocumentList);
-
- // Must do this -before- freeing fFreeWindowList
- TView * theDeskScrapView = gClipboardMgr->fClipOrphanage;
- if (theDeskScrapView != NULL)
- {
- // Because TDeskScrapView overrides Free to -not- call inherited::Free
- theDeskScrapView->TView::Free();
-
- if (gClipboardMgr->fClipView == theDeskScrapView)
- gClipboardMgr->fClipView = NULL;
- gClipboardMgr->fClipOrphanage = NULL;
- }
-
- fFreeWindowList = (TList *) FreeListIfObject(fFreeWindowList);
- }
-
-
- #pragma segment AClose
- void TTidyApplication::CleanUpJustInTimeObjects()
- {
- // gWindows is created on a "just in time" basis by RegisterWindow() in UWindows.cp.
- // It seems to be the only object that MacApp creates in this fashion.
-
- extern TDynamicArray * gWindows;
- gWindows = (TDynamicArray *) FreeIfObject(gWindows);
- }
-
-
- #pragma segment AClose
- pascal void TTidyApplication::Free() // Override
- {
- /******
-
- Some of the objects which MacApp 3.0.1 allocates are freed by TApplication::Close(),
- which is called from TQuitCommand::DoIt(). Some others are freed when CleanUpMacApp()
- calls gApplication->Free(), resulting in calls to TCommandHandler::Free() and
- TEventHandler::Free(). Others are not freed at all; they disappear when the
- application's heap is disposed of after the application terminates.
-
- This set of functions endeavors to clean up what MacApp doesn't. One reason to
- do so is that one can set a breakpoint just before the application terminates
- and learn from the results. Any objects left are objects one's own application
- has allocated but not freed. Some of these may span the life of the application
- and need not be freed except to make the rest stand out (as above); others
- represent likely memory leaks.
-
- *******/
-
- #ifdef qCleanUpAtQuit
- this->CleanUpAfterIApplication();
- this->CleanUpJustInTimeObjects();
-
- DeInitUMacApp();
- #endif
-
- inherited::Free();
-
- // Be sure not to refer to members of gApplication
- // after calling inherited::Free()!
-
- #ifdef qCleanUpAtQuit
- LastChance();
- #endif
- }